package com.shyroke.calculation;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import entity.GroupDesicionDto;

/**
 * 计算群体效用值Si和个体遗憾值Ri
 * @author shyroke
 *
 */
public class SiCal {

	//正理想值
	private List<Double> zhenglixiang= Arrays.asList(1.00,0.00,0.00);
	
	/**
	 * 计算距离
	 * @param TIF TIF的list集合
	 * @return
	 */
	public  double getDistance(List<Double> TIF) {
		
		double result=0;
		
			
		double distanceT =  (TIF.get(0)-zhenglixiang.get(0))*(TIF.get(0)-zhenglixiang.get(0))  ;
		double distanceI =  (TIF.get(1)-zhenglixiang.get(1))*(TIF.get(1)-zhenglixiang.get(1))  ;
		double distanceF =  (TIF.get(2)-zhenglixiang.get(2))*(TIF.get(2)-zhenglixiang.get(2))  ;
		
		double distance = (distanceT+distanceI+distanceF)/3;
		
		result = Math.sqrt(distance);
		return Double.valueOf(new DecimalFormat("#.000").format(result));
	}
	
/**
 * 计算某个方案的群体效用值Si
 * @param attrWeight 属性权重
 * @param distanceList 每个属性的距离的集合,必须是有序的，第一个代表第一个属性的距离，以此类推
 * @return 某个决策方案的Si
 */
	public double getSiByX(Map<String,Double> attrWeight,List<Double> distanceList) {
		
		double result=0;
		
		for(int i=1;i<=distanceList.size();i++) {
			result =result + distanceList.get(i-1)*attrWeight.get("c"+i);
		}
		
		return result;
	}
	
	/**
	 * 计算某个方案的个体遗憾值Ri
	 * @param attrWeight 属性权重
	 * @param distanceList 每个属性的距离的集合,必须是有序的，第一个代表第一个属性的距离，以此类推
	 * @return
	 */
	public double getRiByX(Map<String,Double> attrWeight,List<Double> distanceList) {
		double result = 0;
		
		
		for(int i=1;i<=distanceList.size();i++) {
			double attribute = distanceList.get(i-1)*attrWeight.get("c"+i);
			result = result>attribute?result:attribute;
		}
		
		return result;
	}
	

	/**
	 * 计算群体效用值Si
	 * @param attrWeight
	 * @param groupDesicionDtos
	 * @return <x1,0.402>表示决策方案x1的Si值为0.402
	 */
	public Map<String,Double> getSi(Map<String,Double> attrWeight,List<GroupDesicionDto> groupDesicionDtos) {
		Map<String,Double> resultMap = new HashMap<>();
		
		List<List<Double>> x1TIF = new ArrayList<>();
		List<List<Double>> x2TIF = new ArrayList<>();
		List<List<Double>> x3TIF = new ArrayList<>();

		for(int i=0;i<groupDesicionDtos.size();i++) {
			GroupDesicionDto desicionDto = groupDesicionDtos.get(i);
			x1TIF.add(desicionDto.getX1info());
			x2TIF.add(desicionDto.getX2info());
			x3TIF.add(desicionDto.getX3info());
		}
		
		/**
		 * 第一个决策方案的Si
		 */
		List<Double> getSiParam = new ArrayList<>();
		List<Double> getDisParam = new ArrayList<>();
		for(int i=0;i<x1TIF.size();i++) {
			 getDisParam = x1TIF.get(i);
			Double distance = this.getDistance(getDisParam);
			getSiParam.add(distance);
		}
		double X1Si = this.getSiByX(attrWeight, getSiParam);
		resultMap.put("x1", X1Si);
		
		/**
		 * 第二个决策方案的Si
		 */
		List<Double> getSiParam2 = new ArrayList<>();
		List<Double> getDisParam2 = new ArrayList<>();
		for(int i=0;i<x2TIF.size();i++) {
			getDisParam2 = x2TIF.get(i);
			Double distance = this.getDistance(getDisParam2);
			getSiParam2.add(distance);
		}
		double X2Si = this.getSiByX(attrWeight, getSiParam2);
		resultMap.put("x2", X2Si);
		
		/**
		 * 第三个决策方案的Si
		 */
		List<Double> getSiParam3 = new ArrayList<>();
		List<Double> getDisParam3 = new ArrayList<>();
		for(int i=0;i<x3TIF.size();i++) {
			getDisParam3 = x3TIF.get(i);
			Double distance = this.getDistance(getDisParam3);
			getSiParam3.add(distance);
		}
		double X3Si = this.getSiByX(attrWeight, getSiParam3);
		resultMap.put("x3", X3Si);
		
		return resultMap;
	}
	
	
	/**
	 * 
	 * @param attrWeight
	 * @param groupDesicionDtos
	 * @return <x1,0.402>表示决策方案x1的Ri值为0.402
	 */
	public Map<String,Double> getRi(Map<String,Double> attrWeight,List<GroupDesicionDto> groupDesicionDtos) {
		
		
	Map<String,Double> resultMap = new HashMap<>();
		
		List<List<Double>> x1TIF = new ArrayList<>();
		List<List<Double>> x2TIF = new ArrayList<>();
		List<List<Double>> x3TIF = new ArrayList<>();

		for(int i=0;i<groupDesicionDtos.size();i++) {
			GroupDesicionDto desicionDto = groupDesicionDtos.get(i);
			x1TIF.add(desicionDto.getX1info());
			x2TIF.add(desicionDto.getX2info());
			x3TIF.add(desicionDto.getX3info());
		}
		
		/**
		 * 第一个决策方案的Si
		 */
		List<Double> getSiParam = new ArrayList<>();
		List<Double> getDisParam = new ArrayList<>();
		for(int i=0;i<x1TIF.size();i++) {
			 getDisParam = x1TIF.get(i);
			Double distance = this.getDistance(getDisParam);
			getSiParam.add(distance);
		}
		double X1Si = this.getRiByX(attrWeight, getSiParam);
		resultMap.put("x1", X1Si);
		
		/**
		 * 第二个决策方案的Si
		 */
		List<Double> getSiParam2 = new ArrayList<>();
		List<Double> getDisParam2 = new ArrayList<>();
		for(int i=0;i<x2TIF.size();i++) {
			getDisParam2 = x2TIF.get(i);
			Double distance = this.getDistance(getDisParam2);
			getSiParam2.add(distance);
		}
		double X2Si = this.getRiByX(attrWeight, getSiParam2);
		resultMap.put("x2", X2Si);
		
		/**
		 * 第三个决策方案的Si
		 */
		List<Double> getSiParam3 = new ArrayList<>();
		List<Double> getDisParam3 = new ArrayList<>();
		for(int i=0;i<x3TIF.size();i++) {
			getDisParam3 = x3TIF.get(i);
			Double distance = this.getDistance(getDisParam3);
			getSiParam3.add(distance);
		}
		double X3Si = this.getRiByX(attrWeight, getSiParam3);
		resultMap.put("x3", X3Si);
		
		return resultMap;
		
	}
	
}
